//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

struct AiSetCommonView: View {
    var forceShow = false
    @EnvironmentObject var themeManager: ThemeManager
    @EnvironmentObject var appStorageManager: AppStorageManager

    @State var isShow = false

    var body: some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text("AI设置")
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                    .onTapGesture {
                        withAnimation(Animation.easeInOut(duration: 0.3)) {
                            if !forceShow {
                                isShow.toggle()
                            }
                        }
                    }
                
                if isShow{
                    Toggle("开关", isOn: appStorageManager.$toggleAi)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                        .onChange(of: appStorageManager.toggleAi) { newValue in
                            TaskExt.deleteTask(nil)
                        }
//                    Toggle("表情", isOn: $toggleAiEmoji)
//                        .foregroundColor(.bgItemContent)
//                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
//                        .font(.system(size: 14))
                    
//                    Toggle(isOn: $toggleAiPeriod , label: {
//                        VStack(alignment:.leading){
//                            Text("经期提醒")
//                                .foregroundColor(.bgItemContent)
//                                .font(.system(size: 14))
//                            Text("AI通过记录分析经期时间，进行语音电话提醒，\n会消耗1次回应次数")
//                                .foregroundColor(.bgNormalText4)
//                                .font(.system(size: 10))
//                        }
//                        
//                    }).toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                    HStack {
                        VStack(alignment:.leading){
                            Text("API连接")
                                .foregroundColor(.bgItemContent)
                                .font(.system(size: 14))
                            Text("可自行接入自己的API")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            pathManager.jump(type: JumpType.AI_CUSTOM)
                        }
                    HStack {
                        VStack(alignment:.leading){
                            Text("回应次数")
                                .foregroundColor(.bgItemContent)
                                .font(.system(size: 14))
                            Text("点击查看使用说明")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }
                        Spacer()
                        Text(AiChatExt.getAiCountStr())
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showTipsDialog(title: "回应次数说明", text: highlightText(fullText: "当AI进行日记回复、来电、写信、评论、聊天都会扣除相应的次数，次数用完则无法自动回应；\n\n图片识别2次;\n聊天语音转换扣2次；\n开启记忆注入2次；\n其余扣1次;\n使用自定义api则不扣次数", colorTexts: ["2次","1次"],color: UIColor.bgRed, isBold:true, highlightAll: true) ,alignment: .leading) {
                                
                            }
                        }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18).background(.bgItem).clipShape(RoundedRectangle(cornerRadius: 15))
            .onAppear{
                if forceShow {
                    isShow = true
                }
            }
    }
}

//#Preview {
//    SetPasswordView()
//}

//定义预览提供者
struct AiSetCommon_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiSetCommonView(isShow: true))
    }
}
